package com.cocol.core.base;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cocol.core.bean.Entity;
import com.cocol.core.ioc.ViewInjectUtils;
import com.cocol.core.utils.log.LogUtils;

/**
 * @author Cocol
 * @description 公共Fragment
 * @date 15/12/29 下午3:34
 * @email 617882976@qq.com
 */
public abstract class BaseFragment<T extends Entity> extends Fragment {

    public static final String ARGUMENT = "argument";
    /** ====================== STATE ====================== */
    public static final int STATE_NONE = 0;
    public static final int STATE_REFRESH = 1;
    public static final int STATE_LOAD_MORE = 2;
    public static final int STATE_NO_MORE = 3;
    public static final int STATE_WILL_LOAD = 4;// 正在下拉但还没有到刷新的状态
    public static int mState = STATE_NONE;

    protected View mRootView;

    public View getRootView() {
        return mRootView;
    }

    protected Handler handler = new Handler();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogUtils.trace(getClass().getSimpleName());
        mRootView = ViewInjectUtils.inject(this);
        if (mRootView == null) {// library中不能饮用R文件中的文件，所以只能使用常规的方式来查找控件
            mRootView = LayoutInflater.from(getContext()).inflate(layoutId(), null, false);
            findView(mRootView);
        }
        init(mRootView);
        return mRootView;
    }


    /** =================================在library中使用================================ */
    protected int layoutId(){
        return 0;
    }
    protected void findView(View mRootView) {

    }
    /** ======================================================================== */

    protected abstract void init(View contentView);

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onDetach() {
        super.onDetach();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onStart() {
        super.onStart();
        LogUtils.trace(getClass().getSimpleName());
    }

    @Override
    public void onStop() {
        super.onStop();
        LogUtils.trace(getClass().getSimpleName());
    }
}
